import {Component, OnInit} from '@angular/core';
import {HttpService} from "../share/http.service";
import {message} from "../share/produce-share.service";
import {url} from '../share/url';


@Component({
  moduleId: module.id,
  selector: 'my-member',
  templateUrl: 'member.component.html'
})
export class memberComponent implements OnInit {
  public httpGetMessageUrl:string=url.url+'/app2/web/manage/message/list';
  public httpDeleteMessageUrl:string=url.url+'/app2/web/manage/message/delete';
  public httpGetMessageParams:object = {page:0,size:10,sort:'id'};
  public page: number;
  public size: number;
  public togglePage: number = 4;
  public togglePageArray: Array<number>=[];
  public theads:Array<string>=['id','名字','联系方式','留言时间','操作'];
  public messages:Array<message>=[];
  public viewUniquMessage:Array<message>=[];
  public viewMessageModal:boolean=true;
  public show:boolean = false;
  constructor(private http: HttpService

  ) {
  }

  ngOnInit() {
    // this.messages=[
    //   new message(1,'公司名','我是留言','2017-5-5','我是人名',15824155555),
    //   new message(2,'公司名','我是留言','2017-5-5','我是人名',15824155555),
    //   new message(3,'公司名','我是留言','2017-5-5','我是人名',15824155555),
    //   new message(4,'公司名','我是留言','2017-5-5','我是人名',15824155555),
    // ]
    this.http.httpGet(this.httpGetMessageUrl,this.httpGetMessageParams).subscribe(res => {
        console.log(res);
        this.messages = res.results.messageList.content;
        console.log(this.messages);
        this.togglePage = res.results.messageList.togalPages;
        for (let i = 0; i < this.togglePage; i++) {
          this.togglePageArray.push(i)
        }
        this.show = true;
      })
  }
  // reload(){
  //   this.http.httpGet(this.httpGetMessageUrl,this.httpGetMessageParams)
  //     .subscribe((res) =>{
  //     console.log(res);
  //     this.messages=[
  //       new message(2,'公司名','我是留言','2017-5-5','我是人名',15824155555),
  //       new message(3,'公司名','我是留言','2017-5-5','我是人名',15824155555),
  //       new message(4,'公司名','我是留言','2017-5-5','我是人名',15824155555),
  //     ]
  //   });
  //   console.log('重新调用get请求,获取参数')
  //
  // }
  viewHandle(index){
    this.viewMessageModal=false;
    this.viewUniquMessage = this.messages.filter(item => item.id ===index+1)
  }
  deleteHandle(index){
    console.log(index);
    if(confirm("是否删除")){
      this.http.httpDelete(this.httpDeleteMessageUrl,{id:index})
        .subscribe((res)=>{
          console.log('删除了留言');
          alert("删除成功 ")
        });

    }
    // this.reload();
  }

  closeViewMessageModal(){
    this.viewMessageModal=true;
  }
}
